package com.fessc.develop.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fessc.develop.entity.KeyPropertiesVo;
import com.fessc.develop.handler.SignHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

@Slf4j
@Service
public class VirtualJsonUtil {

    @Resource
    private KeyPropertiesVo keyPropertiesVo;

    @Resource
    private SignHandler signHandler;

    public String readLocalFile(String filePath, String jsonData) {
        InputStreamReader read = null;
        BufferedReader in = null;
        String message = "";
        String serviceCode = "";
        JSONObject jsonObject = new JSONObject();
        try {
            try {
                jsonObject = JSONObject.parseObject(jsonData);
                serviceCode = jsonObject.getJSONObject("SYS_HEAD").getString("SERVICE_CODE");
                if (BusiUtil.isNull(serviceCode)) {
                    serviceCode = "virtualHostFile";
                }
            } catch (Exception e) {
                serviceCode = "virtualHostFile";
            }
            String virtualHostFileName = filePath + serviceCode + ".txt";
            log.info("模拟报文路径：" + virtualHostFileName);
            File virtualHostFile = new File(virtualHostFileName);
            if (!virtualHostFile.exists() && !virtualHostFile.createNewFile()) {
                log.info("创建文件失败");
            }
            read = new InputStreamReader(Files.newInputStream(virtualHostFile.toPath()), StandardCharsets.UTF_8);
            in = new BufferedReader(read);
            String line = null;
            StringBuilder sb = new StringBuilder();
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
            message = sb.toString();
            if (BusiUtil.isNull(message)) {
                return message;
            }
            jsonObject = JSONObject.parseObject(message);
            try {
                JSONObject dataObject = jsonObject.getJSONObject("DATA");
                //String pricer = "D:\\Code\\project\\develop\\private.key";
                //String pubcer = "D:\\Code\\project\\develop\\public.cer";
                //RSATools rsaTools = new RSATools(pricer, pubcer);
                //jsonObject.put("DATA", rsaTools.encrypt(JSON.toJSONString(dataObject)));
                assert dataObject != null;
                if (BusiUtil.isNotNull(dataObject)) {
                    String dataEncrypt = signHandler.encrypt(JSON.toJSONString(dataObject), keyPropertiesVo.getPublicKey());
                    jsonObject.put("DATA", dataEncrypt);
                }
            } catch (Exception e) {
                log.info("模拟报文加解密失败："+ e.getMessage());
            }
        } catch (Exception e) {
            log.info(e.getMessage());
        } finally {
            try {
                if (null != in)
                    in.close();
                if (null != read)
                    read.close();
            } catch (Exception e) {
                log.info(e.getMessage());
            }
        }
        return JSON.toJSONString(jsonObject);
    }

}
